package cn.semcorp.zhuhaizj.controller;

import cn.semcorp.zhuhaizj.common.Common;
import cn.semcorp.zhuhaizj.mapper.UtilizationMapper;
import cn.semcorp.zhuhaizj.pojo.dto.*;
import cn.semcorp.zhuhaizj.pojo.vo.*;
import cn.semcorp.zhuhaizj.restful.JsonResult;
import cn.semcorp.zhuhaizj.services.impl.UtilizationServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


@Slf4j
@RestController
@Validated
@Api(tags = "利用率")
@RequestMapping("/utilization")
public class UtilizationController {
    /**
     * 业务层装配
     */
    @Autowired
    public UtilizationServiceImpl utilizationService;

    /** 重要接口
     * 同步-> 生成利用率- 班次/日次/低
     */
    @PostMapping("/sync")
    @ApiOperation("自定义同步利用率")
    public JsonResult syncUtilization(){
            String date = String.valueOf(Common.getBeforeNowDate());
            int row1 = utilizationService.quartzInsertShiftUtilization(date);
            int row2 = utilizationService.quartzInsertDayUtilization(date);
            int row3 = utilizationService.quartzInsertLowUtilizationDay(date);
            if (row1 == 0 || row2 == 0 || row3 == 0){
                return JsonResult.failed(date+"同步失败,数据已同步或不存在可同步数据!");
            }
            return JsonResult.ok("同步成功!");
    }

    /** 重要接口
     * 上传涂布产出表
     */
    @PostMapping("/updateCoating")
    public JsonResult updateCoating(@RequestBody List<CoatingDTO> list){
            utilizationService.updateCoating(list);
            return JsonResult.ok();
    }

    /** 重要接口
     * 上传制浆领用表
     */
    @PostMapping("/updatePulping")
    public JsonResult updatePulping(@RequestBody List<PulpingDTO> list){
            utilizationService.updatePulping(list);
            return JsonResult.ok();
    }


    /**
     * 机台改善 - 查询修改信息
     * @param id
     * @return
     */
    @GetMapping("/selectLowUtilizationById/{id}")
    public JsonResult selectLowUtilizationById(@PathVariable String id){
            return JsonResult.ok(utilizationService.selectLowUtilizationById(id));
    }

    /**
     * 机台改善 - 修改原因、措施
     * @param utilizationDTO
     * @return
     */
    @PostMapping("/updateLowUtilizationDayResultMeasure")
    public JsonResult updateLowUtilizationDayResultMeasure(UpdateLowUtilizationDTO utilizationDTO){
            utilizationService.updateLowUtilizationDayResultMeasure(utilizationDTO);
            return JsonResult.ok("更新成功!");
    }

    /**
     * 数据核对 - 初始化 - 查询涂布班利用率
     * @return
     */
    @GetMapping("/selectUtilizationShift")
    public JsonResult selectUtilizationShift() {
            return JsonResult.ok(utilizationService.selectUtilizationShift());
    }

    /**
     * 单系列查询 - 根据涂布车间查询机台列表
     */
    @GetMapping("/selectMachineByWorkshop/{workshop}")
    public JsonResult selectMachineByWorkshop(@PathVariable String workshop){
           return JsonResult.ok(utilizationService.selectMachineByWorkshop(workshop));
    }

    /**
     * 单系列查询 - 初始化 - 查询机台列表
     * @return
     */
    @GetMapping("/selectMachineList")
    public JsonResult selectMachine () {
            return JsonResult.ok(utilizationService.selectMachine());
    }

    /**
     * 单系列查询 - 初始化 - 查询车间列表
     * @return
     */
    @GetMapping("/selectWorkshopList")
    public JsonResult selectWorkshopList(){
        //<List<WorkshopVO>>
            return JsonResult.ok(utilizationService.selectWorkshopList());
    }

    @GetMapping("/select/coating/workshop")
    public JsonResult selectCoatingWorkshop(){
        //<List<WorkshopVO>>
            return JsonResult.ok(utilizationService.selectCoatingWorkshop());
    }


    /**
     * 数据核对 - <查询>全部方法
     */
    @GetMapping("/clickQuery")
    public JsonResult clickQuery(ClickQueryDTO dto){
        return JsonResult.ok(utilizationService.clickQuery(dto),"查询成功!");
    }

    /**
     * 机台改善 - 时间查询
     * @return
     */
    @GetMapping("/queryMeasureByRangTime")
    public JsonResult queryMeasureByRangTime(QueryMeasureDTO dto) {
            return JsonResult.ok(utilizationService.queryMeasureByRangTime(dto));
    }

    /**
     * 1
     * @return
     */
    @GetMapping("/selectUtilizationDayAll")
    public JsonResult selectUtilizationTenDayAll(){
            return JsonResult.ok(utilizationService.queryAllDayUtilization());
    }

    @GetMapping("/queryAllWeekUtilization")
    public JsonResult queryAllWeekUtilization(){
            return JsonResult.ok(utilizationService.queryAllWeekUtilization());
    }

    @GetMapping("/queryAllMonthUtilization")
    public JsonResult queryAllMonthUtilization(){
            return JsonResult.ok(utilizationService.queryAllMonthUtilization());
    }

    /**
     * 2
     */
    @GetMapping("/queryAllWorkshopUtilization/{date}")
    public JsonResult queryAllWorkshopUtilization(@PathVariable String date){
            return JsonResult.ok(utilizationService.queryAllWorkshopUtilization(date));
    }

    @GetMapping("/queryAllWeekWorkshopUtilization/{week}")
    public JsonResult queryAllWeekWorkshopUtilization(@PathVariable String week){
            return JsonResult.ok(utilizationService.queryAllWeekWorkshopUtilization(week));
    }

    @GetMapping("/queryAllWeekMachineUtilization")
    public JsonResult queryAllWeekMachineUtilization(String week,String workshop){
            return JsonResult.ok(utilizationService.queryAllWeekMachineUtilization(week, workshop));
    }


    /**
     * 3
     */
    @GetMapping("/queryAllMachineUtilization")
    public JsonResult queryAllMachineUtilization(String workshop,String date){
            return JsonResult.ok(utilizationService.queryAllMachineUtilization(workshop, date));
    }

    @GetMapping("/queryAllMonthWorkshopUtilization/{yearMonth}")
    public JsonResult queryAllMonthWorkshopUtilization(@PathVariable String yearMonth){
        //<List<SelectAllUtilizationMonthWorkshopFigVO>>
            return JsonResult.ok(utilizationService.queryAllMonthWorkshopUtilization(yearMonth));
    }

    @GetMapping("/queryAllMonthMachineUtilization")
    public JsonResult queryAllMonthMachineUtilization(String yearMonth,String workshop){
        //<List<SelectAllUtilizationMonthMachineFigVO>>
            return JsonResult.ok(utilizationService.queryAllMonthMachineUtilization(yearMonth, workshop));
    }

    /**
     * 查询涂布综合周类型利用率
     * @return
     */
    @GetMapping("/queryAllWeekModelUtilization/{slurryModel}")
    public JsonResult queryAllWeekModelUtilization(@PathVariable String slurryModel){
        //<List<SelectAllUtilizationWeekModelFigVO>>
            return JsonResult.ok(utilizationService.queryAllWeekModelUtilization(slurryModel));
    }

    /**
     * 查询涂布综合月类型利用率
     * @return
     */
    @GetMapping("/queryAllMonthModelUtilization/{slurryModel}")
    public JsonResult queryAllMonthModelUtilization(@PathVariable String slurryModel){
            return JsonResult.ok(utilizationService.queryAllMonthModelUtilization(slurryModel));
    }

    /** 单系列查询 5 - 1
     * 图表 - 条件查询日次类型
     * @param
     * @return
     */
    @PostMapping("/querySeriesUtilizationDayFigVO")
    public JsonResult querySeriesUtilizationDayFigVO(SelectSeriesUtilizationDayFigDTO dto){
            return JsonResult.ok(utilizationService.querySeriesUtilizationDayFigVO(dto));
    }


    /** 单系列查询 5 - 1
     * 图表 - 初始化
     * @param
     * @return
     */
    @GetMapping("/loadQuerySeriesUtilizationDayFigVO")
    public JsonResult loadQuerySeriesUtilizationDayFigVO(){
        //<List<SelectSeriesUtilizationFigVO>>
            return JsonResult.ok(utilizationService.loadQuerySeriesUtilizationDayFigVO());
    }

    /** 单系列查询 5 - 2
     * 图表 - 条件查询日次类型
     * @param
     * @return
     */
    @GetMapping("/querySeriesUtilizationWeekFigVO")
    public JsonResult querySeriesUtilizationWeekFigVO(String workshop,String machine){
            return JsonResult.ok(utilizationService.querySeriesUtilizationWeekFigVO(workshop,machine));
    }


    /** 单系列查询 5 - 3
     * 图表 - 条件查询月次类型
     * @param
     * @return
     */
    @GetMapping("/querySeriesUtilizationMonthFigVO")
    public JsonResult querySeriesUtilizationMonthFigVO(String workshop,String machine){
        //<List<SelectSeriesUtilizationFigVO>>
            return JsonResult.ok(utilizationService.querySeriesUtilizationMonthFigVO(workshop,machine));
    }


    @GetMapping("/selectLossByMachine/{machine}")
    public JsonResult selectLossByMachine(@PathVariable String machine){
        //<List<SelectMachineLossVO>>
            return JsonResult.ok(utilizationService.selectLossByMachine(machine));
    }

    @GetMapping("/queryBasicByDateRangAndModel")
    public JsonResult queryBasicByDateRangAndModel(String dateStart,String dateEnd,String model){
        //<List<T>>
            return JsonResult.ok(utilizationService.queryBasicByDateRangAndModel(dateStart, dateEnd, model));
    }

}
